home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / snmp / mib2c-data / mfd-access-container-cached-defines.m2i < prev    next >
Encoding:
Text File  |  2007-02-07  |  15.3 KB  |  489 lines

  1. ############################################################  -*- c -*-
  2. ###generic include for XXX. Do not use directly.
  3. ###
  4. ### $Id: mfd-access-container-cached-defines.m2i,v 1.16.2.1 2005/01/06 14:51:52 rstory Exp $
  5. ########################################################################
  6. @if $m2c_mark_boundary == 1@
  7. /** START code generated by $RCSfile: mfd-access-container-cached-defines.m2i,v $ $Revision: 1.16.2.1 $ */
  8. @end@
  9. ##//####################################################################
  10. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  11. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  12. @if $m2c_processing_type eq 'h'@
  13.  
  14. /*
  15.  * TODO:180:o: Review $context cache timeout.
  16.  * The number of seconds before the cache times out
  17.  */
  18. #define $context.uc_CACHE_TIMEOUT   60
  19.  
  20. void ${context}_container_init(netsnmp_container **container_ptr_ptr,
  21.                              netsnmp_cache *cache);
  22. int ${context}_cache_load(netsnmp_container *container);
  23. void ${context}_cache_free(netsnmp_container *container);
  24.  
  25. @   if $m2c_include_examples == 1@
  26. $example_start
  27. /* *********************************************************************
  28.  * Since we have no idea how you really access your data, we'll go with
  29.  * a worst case example: a flat text file.
  30.  @   if $m2c_data_transient != 2@
  31.  @      print Example code is for fully transient data. Either turn off@
  32.  @      print m2c_include_examples or set m2c_data_transient to 2.@
  33.  @      exit@
  34.  @   end@
  35.  */
  36. #define MAX_LINE_SIZE 256
  37. $example_end
  38. @   end@ // example
  39. @end@ // m2c_processing_type eq 'h'
  40. ##//####################################################################
  41. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  42. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  43. @if $m2c_processing_type eq 'c'@
  44. /**
  45.  * container-cached overview
  46.  *
  47.  */
  48.  
  49. /***********************************************************************
  50.  *
  51.  * cache
  52.  *
  53.  ***********************************************************************/
  54. /**
  55.  * container initialization
  56.  *
  57.  * @param container_ptr_ptr A pointer to a container pointer. If you
  58.  *        create a custom container, use this parameter to return it
  59.  *        to the MFD helper. If set to NULL, the MFD helper will
  60.  *        allocate a container for you.
  61.  * @param  cache A pointer to a cache structure. You can set the timeout
  62.  *         and other cache flags using this pointer.
  63.  *
  64.  *  This function is called at startup to allow you to customize certain
  65.  *  aspects of the access method. For the most part, it is for advanced
  66.  *  users. The default code should suffice for most cases. If no custom
  67.  *  container is allocated, the MFD code will create one for your.
  68.  *
  69.  *  This is also the place to set up cache behavior. The default, to
  70.  *  simply set the cache timeout, will work well with the default
  71.  *  container. If you are using a custom container, you may want to
  72.  *  look at the cache helper documentation to see if there are any
  73.  *  flags you want to set.
  74.  *
  75.  * @remark
  76.  *  This would also be a good place to do any initialization needed
  77.  *  for you data source. For example, opening a connection to another
  78.  *  process that will supply the data, opening a database, etc.
  79.  */
  80. void
  81. ${context}_container_init(netsnmp_container **container_ptr_ptr,
  82.                         netsnmp_cache *cache)
  83. {
  84.     DEBUGMSGTL(("verbose:${context}:${context}_container_init","called\n"));
  85.     
  86.     if((NULL == cache) || (NULL == container_ptr_ptr)) {
  87.         snmp_log(LOG_ERR,"bad params to ${context}_container_init\n");
  88.         return;
  89.     }
  90.  
  91.     /*
  92.      * For advanced users, you can use a custom container. If you
  93.      * do not create one, one will be created for you.
  94.      */
  95.     *container_ptr_ptr = NULL;
  96.  
  97.     /*
  98.      * TODO:345:A: Set up $context cache properties.
  99.      *
  100.      * Also for advanced users, you can set parameters for the
  101.      * cache. Do not change the magic pointer, as it is used
  102.      * by the MFD helper. To completely disable caching, set
  103.      * cache->enabled to 0.
  104.      */
  105.     cache->timeout = $context.uc_CACHE_TIMEOUT; /* seconds */
  106. } /* ${context}_container_init */
  107.  
  108. /**
  109.  * load cache data
  110.  *
  111.  * TODO:350:M: Implement $context cache load
  112.  *
  113.  * @param container container to which items should be inserted
  114.  *
  115.  * @retval MFD_SUCCESS              : success.
  116.  * @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source
  117.  * @retval MFD_ERROR                : other error.
  118.  *
  119.  *  This function is called to cache the index(es) (and data, optionally)
  120.  *  for the every row in the data set.
  121.  *
  122.  * @remark
  123.  *  While loading the cache, the only important thing is the indexes.
  124.  *  If access to your data is cheap/fast (e.g. you have a pointer to a
  125.  *  structure in memory), it would make sense to update the data here.
  126.  *  If, however, the accessing the data invovles more work (e.g. parsing
  127.  *  some other existing data, or peforming calculations to derive the data),
  128.  *  then you can limit yourself to setting the indexes and saving any
  129.  *  information you will need later. Then use the saved information in
  130.  *  ${context}_row_prep() for populating data.
  131.  *
  132.  * @note
  133.  *  If you need consistency between rows (like you want statistics
  134.  *  for each row to be from the same time frame), you should set all
  135.  *  data here.
  136.  *
  137.  */
  138. int
  139. ${context}_cache_load(netsnmp_container *container)
  140. {
  141.     ${context}_rowreq_ctx *rowreq_ctx;
  142.     size_t                 count = 0;
  143. @if $m2c_include_examples == 1@
  144.     
  145.     /*
  146.      * this example code is based on a data source that is a
  147.      * text file to be read and parsed.
  148.      */
  149.     FILE *filep;
  150.     char line[MAX_LINE_SIZE];
  151.     /*
  152.      * temporary storage for index values
  153.      */
  154. @  foreach $node index@
  155. @    include m2c_setup_node.m2i@
  156.         /*
  157.          * $m2c_node_summary
  158.          */
  159. @    if $m2c_node_needlength == 1@
  160. @        eval $m2c_gi_maxlen = (126 - $node.oidlength - $m2c_gi_others)@
  161. @        if $m2c_node_maxlen > $m2c_gi_maxlen@
  162. @            eval $m2c_node_maxlen = $m2c_gi_maxlen@
  163.         /** 128 - 1(entry) - 1(col) - $m2c_gi_others(other indexes) = $m2c_node_maxlen */
  164. @        end@
  165. @    end@ # needlength
  166. @    include node-storage.m2i@
  167. @  end@ // foreach
  168. @end@ // examples
  169.  
  170.     DEBUGMSGTL(("verbose:${context}:${context}_cache_load","called\n"));
  171.  
  172. @if $m2c_include_examples == 1@
  173. $example_start
  174.     /*
  175.      * open our data file.
  176.      */
  177.     filep = fopen("/etc/dummy.conf", "r");
  178.     if(NULL ==  filep) {
  179.         return MFD_RESOURCE_UNAVAILABLE;
  180.     }
  181.  
  182. $example_end
  183. @end@ // example
  184.     /*
  185.      * TODO:351:M: |-> Load/update data in the $context container.
  186.      * loop over your $context data, allocate a rowreq context,
  187.      * set the index(es) [and data, optionally] and insert into
  188.      * the container.
  189.      */
  190.     while( 1 ) {
  191. @   if $m2c_include_examples == 0@
  192.         /*
  193.          * check for end of data; bail out if there is no more data
  194.          */
  195.         if( 1 )
  196.             break;
  197. @   else@
  198. $example_start
  199.     /*
  200.      * get a line (skip blank lines)
  201.      */
  202.     do {
  203.         if (!fgets(line, sizeof(line), filep)) {
  204.             /* we're done */
  205.             fclose(filep);
  206.             filep = NULL;
  207.         }
  208.     } while (filep && (line[0] == '\n'));
  209.  
  210.     /*
  211.      * check for end of data
  212.      */
  213.     if(NULL == filep)
  214.         break;
  215.  
  216.     /*
  217.      * parse line into variables
  218.      */
  219. $example_end
  220. @    end@ # example
  221.  
  222.         /*
  223.          * TODO:352:M: |   |-> set indexes in new $context rowreq context.
  224. @   eval $m2c_tmp = ""@
  225. @   if ($m2c_data_allocate == 1) || ($m2c_data_init == 1)@
  226. @      eval $m2c_tmp = "NULL"@
  227. @      if ($m2c_data_allocate == 1) && ($m2c_data_init == 1)@
  228. @         eval $m2c_tmp = "$m2c_tmp, NULL"@
  229.          * data context will be set from the first param (unless NULL,
  230.          *      in which case a new data context will be allocated)
  231.          * the second param will be passed, with the row context, to
  232.          *      ${context}rowreq_ctx_init.
  233. @      else@
  234.          * data context will be set from the param (unless NULL,
  235.          *      in which case a new data context will be allocated)
  236. @      @end@
  237. @   end@
  238.          */
  239.         rowreq_ctx = ${context}_allocate_rowreq_ctx($m2c_tmp);
  240.         if (NULL == rowreq_ctx) {
  241.             snmp_log(LOG_ERR, "memory allocation failed\n");
  242.             return MFD_RESOURCE_UNAVAILABLE;
  243.         }
  244.         if(MFD_SUCCESS != ${context}_indexes_set(rowreq_ctx
  245. @   foreach $node index@
  246. @      include m2c_setup_node.m2i@
  247. @        if $m2c_node_needlength == 1@
  248.                                , $node, ${node}_len
  249. @        else@
  250.                                , $node
  251. @        end@
  252. @   end@ # foreach index
  253.                )) {
  254.             snmp_log(LOG_ERR,"error setting index while loading "
  255.                      "${context} cache.\n");
  256.             ${context}_release_rowreq_ctx(rowreq_ctx);
  257.             continue;
  258.         }
  259.  
  260.         /*
  261.          * TODO:352:r: |   |-> populate $context data context.
  262.          * Populate data context here. (optionally, delay until row prep)
  263.          */
  264. @if $m2c_data_transient == 0@ # persistent
  265.     /* non-TRANSIENT data: no need to copy. set pointer to data */
  266. @else@
  267.     /*
  268.      * TRANSIENT or semi-TRANSIENT data:
  269.      * copy data or save any info needed to do it in row_prep.
  270.      */
  271. @   foreach $node nonindex@
  272. @      include m2c_setup_node.m2i@
  273.     /*
  274.      * setup/save data for $node
  275.      * $m2c_node_summary
  276.      */
  277. @      if "$m2c_data_context" eq "generated"@
  278. @         eval $m2c_ctx_lh = "$m2c_ctx_rh"@
  279. @         eval $m2c_ctx_lhs = "$m2c_ctx_rhs"@
  280. @         eval $m2c_ctx_rh = "$node"@
  281. @         eval $m2c_ctx_rhs = "${node}_len"@
  282. @         include generic-value-map.m2i@
  283.     
  284. @      end@ # data_context ! generated
  285. @   end@ // for each
  286. @end@ # transient
  287.         
  288.         /*
  289.          * insert into table container
  290.          */
  291.         CONTAINER_INSERT(container, rowreq_ctx);
  292.         ++count;
  293.     }
  294. @if $m2c_include_examples == 1@
  295.  
  296. $example_start
  297.     if(NULL != filep)
  298.         fclose(filep);
  299. $example_end
  300. @end@ # example
  301.  
  302.     DEBUGMSGT(("verbose:${context}:${context}_cache_load",
  303.                "inserted %d records\n", count));
  304.  
  305.     return MFD_SUCCESS;
  306. } /* ${context}_cache_load */
  307.  
  308. /**
  309.  * cache clean up
  310.  *
  311.  * @param container container with all current items
  312.  *
  313.  *  This optional callback is called prior to all
  314.  *  item's being removed from the container. If you
  315.  *  need to do any processing before that, do it here.
  316.  *
  317.  * @note
  318.  *  The MFD helper will take care of releasing all the row contexts.
  319. @   if ($m2c_data_allocate == 1) && ($m2c_data_transient == 0)@
  320.  *  If you did not pass a data context pointer when allocating
  321.  *  the rowreq context, the one that was allocated will be deleted.
  322.  *  If you did pass one in, it will not be deleted and that memory
  323.  *  is your responsibility.
  324. @   end@
  325.  *
  326.  */
  327. void
  328. ${context}_cache_free(netsnmp_container *container)
  329. {
  330.     DEBUGMSGTL(("verbose:${context}:${context}_cache_free","called\n"));
  331.  
  332.     /*
  333.      * TODO:380:M: Free $context cache.
  334.      */
  335. } /* ${context}_cache_free */
  336.  
  337. @end@ // m2c_processing_type eq 'c'
  338. ########################################################################
  339. ##//####################################################################
  340. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  341. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  342. @if $m2c_processing_type eq 'i'@
  343. /**
  344.  * @internal
  345.  */
  346. static int
  347. _cache_load(netsnmp_cache *cache, void *vmagic)
  348. {
  349.     DEBUGMSGTL(("internal:${context}:_cache_load","called\n"));
  350.  
  351.     if((NULL == cache) || (NULL == cache->magic)) {
  352.         snmp_log(LOG_ERR, "invalid cache for ${context}_cache_load\n");
  353.         return -1;
  354.     }
  355.  
  356.     /** should only be called for an invalid or expired cache */
  357.     netsnmp_assert((0 == cache->valid) || (1 == cache->expired));
  358.     
  359.     /*
  360.      * call user code
  361.      */
  362.     return ${context}_cache_load((netsnmp_container*)cache->magic);
  363. } /* _cache_load */
  364.  
  365. /**
  366.  * @internal
  367.  */
  368. static void
  369. _cache_item_free(${context}_rowreq_ctx *rowreq_ctx, void *context)
  370. {
  371.     DEBUGMSGTL(("internal:${context}:_cache_item_free","called\n"));
  372.  
  373.     if(NULL == rowreq_ctx)
  374.         return;
  375.  
  376.     ${context}_release_rowreq_ctx(rowreq_ctx);
  377. } /* _cache_item_free */
  378.  
  379. /**
  380.  * @internal
  381.  */
  382. static void
  383. _cache_free(netsnmp_cache *cache, void *magic)
  384. {
  385.     netsnmp_container *container;
  386.  
  387.     DEBUGMSGTL(("internal:${context}:_cache_free","called\n"));
  388.  
  389.     if((NULL == cache) || (NULL == cache->magic)) {
  390.         snmp_log(LOG_ERR, "invalid cache in ${context}_cache_free\n");
  391.         return;
  392.     }
  393.  
  394.     container = (netsnmp_container*)cache->magic;
  395.  
  396.     /*
  397.      * call user code
  398.      */
  399.     ${context}_cache_free(container);
  400.     
  401.     /*
  402.      * free all items. inefficient, but easy.
  403.      */
  404.     CONTAINER_CLEAR(container,
  405.                     (netsnmp_container_obj_func *)_cache_item_free,
  406.                     NULL);
  407. } /* _cache_free */
  408.  
  409. /**
  410.  * @internal
  411.  * initialize the iterator container with functions or wrappers
  412.  */
  413. void
  414. _${context}_container_init(${context}_interface_ctx *if_ctx)
  415. {
  416.     DEBUGMSGTL(("internal:${context}:_${context}_container_init","called\n"));
  417.     
  418. @    if 0@
  419.     /*
  420.      * find cache
  421.      */
  422.     if_ctx->cache =
  423.         netsnmp_cache_find_by_oid(PARTNER_oid, OID_LENGTH(PARTNER_oid));
  424. @    else@
  425.     /*
  426.      * set up the cache
  427.      */
  428.     if_ctx->cache = netsnmp_cache_create(30, /* timeout in seconds */
  429.                                          _cache_load, _cache_free,
  430.                                          ${context}_oid,
  431.                                          ${context}_oid_size);
  432. @    end@ // shared cache
  433.  
  434.     if(NULL == if_ctx->cache) {
  435.         snmp_log(LOG_ERR, "error creating cache for ${context}\n");
  436.         return;
  437.     }
  438.  
  439.     if_ctx->cache->flags = NETSNMP_CACHE_DONT_INVALIDATE_ON_SET;
  440.  
  441.     ${context}_container_init(&if_ctx->container, if_ctx->cache);
  442.     if(NULL == if_ctx->container)
  443.         if_ctx->container = netsnmp_container_find("${context}:table_container");
  444.     if(NULL == if_ctx->container) {
  445.         snmp_log(LOG_ERR,"error creating container in "
  446.                  "${context}_container_init\n");
  447.         return;
  448.     }
  449.     if_ctx->cache->magic = (void*)if_ctx->container;
  450. } /* _${context}_container_init */
  451.  
  452. @end@ // m2c_processing_type eq 'i'
  453. ########################################################################
  454. ##//####################################################################
  455. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  456. ##//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  457. @if $m2c_processing_type eq 'r'@
  458. ##
  459.   container-cached summary
  460.   ------------------------
  461.     The container-cached data access code is for cases when you want to
  462.     cache your data in the agent/sub-agent.
  463.  
  464.     ... to be continued...
  465.  
  466.  
  467. ########################################################################
  468.   Updating the Index
  469.   ------------------
  470.     TODO : update index for the raw data
  471.     FUNC : ${context}_indexes_set
  472.     WHERE: ${context}_data_access.c
  473.  
  474.     This is a convenience function for setting the index context from
  475.     the native C data. Where necessary, value mapping should be done.
  476.  
  477.     @if $mfd_readme_verbose == 1@
  478.     This function should update the table index values (found in
  479.     tbl_idx) for the given raw data.
  480.  
  481.     @end@
  482.  
  483. @end@ // m2c_processing_type eq 'r'
  484. ########################################################################
  485. ##//####################################################################
  486. @if $m2c_mark_boundary == 1@
  487. /** END code generated by $RCSfile: mfd-access-container-cached-defines.m2i,v $ $Revision: 1.16.2.1 $ */
  488. @end@
  489.